{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1f0527f4-4224-4bcf-92c2-33a22eb31d9c",
   "metadata": {},
   "source": [
    "## 实验2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e43b495a-1f43-440a-aec0-6ec3fe47c468",
   "metadata": {},
   "source": [
    "### 任务1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "541fbd56-f318-4918-8aa5-87476f68732e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入你的身高（米）： 1.69\n",
      "请输入你的体重（公斤）： 55\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bmi数值为：19.26，BMI指标为：国际“正常”，国内“正常”\n"
     ]
    }
   ],
   "source": [
    "class BMICalculator:    \n",
    "    def __init__(self, height, weight):\n",
    "        # 初始化方法，接收身高(米)和体重(千克)\n",
    "        self.height = height  # 身高，单位：米\n",
    "        self.weight = weight  # 体重，单位：千克\n",
    "        self.bmi = self.calculate_bmi()  # 计算BMI值\n",
    "    \n",
    "    def calculate_bmi(self):\n",
    "        # 计算BMI值，公式：BMI = 体重 / 身高²\n",
    "        if self.height <= 0:\n",
    "            raise ValueError(\"身高必须大于0\")\n",
    "        return round(self.weight / (self.height **2), 2)\n",
    "    \n",
    "    def get_international_category(self):\n",
    "        # 根据国际标准判断BMI指标\n",
    "        if self.bmi < 18.5:\n",
    "            return \"偏瘦\"\n",
    "        elif 18.5 <= self.bmi < 25:\n",
    "            return \"正常\"\n",
    "        elif 25 <= self.bmi < 30:\n",
    "            return \"超重\"\n",
    "        else:  # BMI >= 30\n",
    "            return \"肥胖\"\n",
    "    \n",
    "    def get_domestic_category(self):\n",
    "        # 根据国内标准判断BMI指标\n",
    "        if self.bmi < 18.5:\n",
    "            return \"偏瘦\"\n",
    "        elif 18.5 <= self.bmi < 24:\n",
    "            return \"正常\"\n",
    "        elif 24 <= self.bmi < 28:\n",
    "            return \"偏胖\"\n",
    "        else:  # BMI >= 28\n",
    "            return \"肥胖\"\n",
    "    \n",
    "    @staticmethod\n",
    "    def get_user_input():\n",
    "        # 从键盘获取用户的身高和体重输入\n",
    "        while True:\n",
    "            try:\n",
    "                height = float(input(\"请输入你的身高（米）：\"))\n",
    "                if height <= 0:\n",
    "                    print(\"身高必须大于0，请重新输入！\")\n",
    "                    continue\n",
    "                \n",
    "                weight = float(input(\"请输入你的体重（公斤）：\"))\n",
    "                if weight <= 0:\n",
    "                    print(\"体重必须大于0，请重新输入！\")\n",
    "                    continue\n",
    "                \n",
    "                return height, weight\n",
    "            except ValueError:\n",
    "                print(\"输入无效，请输入数字！\")\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 获取用户输入\n",
    "    height, weight = BMICalculator.get_user_input()\n",
    "    \n",
    "    # 创建BMI计算器实例\n",
    "    bmi_calculator = BMICalculator(height, weight)\n",
    "    \n",
    "    # 按照指定模板显示结果\n",
    "    print(f\"bmi数值为：{bmi_calculator.bmi}，BMI指标为：国际“{bmi_calculator.get_international_category()}”，国内“{bmi_calculator.get_domestic_category()}”\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f5c0ba3-2927-4715-b776-92b2614414d6",
   "metadata": {},
   "source": [
    "### 任务2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "997cc2ea-b2f8-4c00-9e0f-0ea39073a68d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入十进制整数： 169\n",
      "请输入转换进制： 8\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "十进制数：169=（2 5 1）8进制\n"
     ]
    }
   ],
   "source": [
    "class DecimalConverter:\n",
    "    # 十进制转换器类，用于将十进制整数转换为二进制或八进\n",
    "    \n",
    "    def __init__(self, decimal_num, base):\n",
    "        \"\"\"初始化方法，接收十进制数和目标进制\"\"\"\n",
    "        self.decimal_num = decimal_num  # 十进制数\n",
    "        self.base = base  # 目标进制，2或8\n",
    "        self.digits = self.convert()  # 转换后的数字列表\n",
    "    \n",
    "    def convert(self):\n",
    "        # 使用while循环将十进制数转换为指定进制\n",
    "        if self.decimal_num == 0:\n",
    "            return [0]\n",
    "            \n",
    "        digits = []\n",
    "        num = self.decimal_num\n",
    "        \n",
    "        # 使用while循环进行转换\n",
    "        while num > 0:\n",
    "            remainder = num % self.base  # 求余数\n",
    "            digits.append(remainder)    # 保存余数\n",
    "            num = num // self.base      # 更新商为新的被除数\n",
    "        \n",
    "        # 反转得到正确的顺序\n",
    "        return digits[::-1]\n",
    "    \n",
    "    def get_result_string(self):\n",
    "        # 将数字列表转换为带空格的字符串\n",
    "        digits_str = \" \".join(map(str, self.digits))\n",
    "        return f\"十进制数：{self.decimal_num}=（{digits_str}）{self.base}进制\"\n",
    "\n",
    "    def get_user_input():\n",
    "        # 获取用户输入的十进制数和目标进制\n",
    "        while True:\n",
    "            try:\n",
    "                # 获取十进制数\n",
    "                decimal_num = int(input(\"请输入十进制整数：\"))\n",
    "                if decimal_num < 0:\n",
    "                    print(\"请输入非负整数！\")\n",
    "                    continue\n",
    "                \n",
    "                # 获取转换进制\n",
    "                base = int(input(\"请输入转换进制：\"))\n",
    "                if base not in [2, 8]:\n",
    "                    print(\"请输入2或8作为转换进制！\")\n",
    "                    continue\n",
    "                \n",
    "                return decimal_num, base\n",
    "            except ValueError:\n",
    "                print(\"输入无效，请输入整数！\")\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 获取用户输入\n",
    "    decimal_num, base = DecimalConverter.get_user_input()\n",
    "    \n",
    "    # 创建转换器实例并转换\n",
    "    converter = DecimalConverter(decimal_num, base)\n",
    "    \n",
    "    # 显示结果\n",
    "    print(converter.get_result_string())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18101e0b-0db3-4bc9-8c17-97df7441521d",
   "metadata": {},
   "source": [
    "### 任务3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc0eb7f6-974d-4b05-8ae9-05675f9772e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "import time\n",
    "\n",
    "def remind_weishao_checkin(interval=60):\n",
    "    \"\"\"\n",
    "    定时提醒用户进行微哨打卡\n",
    "    \n",
    "    参数:\n",
    "        interval: 提醒间隔时间(秒)，默认60秒\n",
    "    \"\"\"\n",
    "    # 外层循环：控制程序持续运行\n",
    "    while True:\n",
    "        # 获取当前时间\n",
    "        current_time = datetime.datetime.now()\n",
    "        # 格式化时间字符串\n",
    "        time_str = current_time.strftime(\"%Y-%m-%d %H:%M:%S.%f\")[:-3]\n",
    "        \n",
    "        # 内层循环：可以根据需要添加更多提醒逻辑\n",
    "        while True:\n",
    "            # 显示提醒信息\n",
    "            print(f\"{time_str}   哥们，要微哨打卡啦\")\n",
    "            break  # 内层循环只执行一次\n",
    "            \n",
    "        # 等待指定时间后再次提醒\n",
    "        time.sleep(interval)\n",
    "        \n",
    "        # 可以添加退出条件，例如按特定键退出\n",
    "        # 这里为了演示简单，不添加退出条件，可按Ctrl+C强制退出\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 设置提醒间隔为60秒，可根据需要调整\n",
    "    remind_weishao_checkin(60)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f177201-5dae-4192-88eb-29e0c8fe666e",
   "metadata": {},
   "source": [
    "### 任务4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dddb14fa-336c-4936-840b-cc1795e3c85e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "class PerfectNumberFinder:\n",
    "    def __init__(self, max_limit):\n",
    "        \"\"\"初始化完全数查找器\"\"\"\n",
    "        self.max_limit = max_limit  # 查找的最大范围\n",
    "        self.perfect_numbers = []  # 存储找到的完全数及其因子\n",
    "    \n",
    "    def get_proper_factors(self, number):\n",
    "        \"\"\"获取一个数的所有真因子（不包括自身）\"\"\"\n",
    "        factors = []\n",
    "        for i in range(1, number):\n",
    "            if number % i == 0:\n",
    "                factors.append(i)\n",
    "        return factors\n",
    "    \n",
    "    def is_perfect(self, number):\n",
    "        \"\"\"判断一个数是否为完全数\"\"\"\n",
    "        if number <= 0:\n",
    "            return False, []\n",
    "        \n",
    "        factors = self.get_proper_factors(number)\n",
    "        return sum(factors) == number, factors\n",
    "    \n",
    "    def find_all(self):\n",
    "        \"\"\"查找指定范围内的所有完全数\"\"\"\n",
    "        start_time = time.time()\n",
    "        \n",
    "        # 遍历范围内的每个数\n",
    "        for num in range(1, self.max_limit + 1):\n",
    "            is_perfect, factors = self.is_perfect(num)\n",
    "            if is_perfect:\n",
    "                self.perfect_numbers.append((num, factors))\n",
    "        \n",
    "        self.elapsed_time = time.time() - start_time\n",
    "        return self.perfect_numbers\n",
    "    \n",
    "    def display_results(self):\n",
    "        \"\"\"展示查找结果\"\"\"\n",
    "        for num, factors in self.perfect_numbers:\n",
    "            # 格式化真因子列表为字符串\n",
    "            factors_str = \", \".join(map(str, factors))\n",
    "            print(f\"{num} 是完全数，真因子为 [{factors_str}]\")\n",
    "        \n",
    "        # 显示总耗时\n",
    "        print(f\"计算完成，共用时{self.elapsed_time:.1f}秒\")\n",
    "\n",
    "# 程序执行入口\n",
    "if __name__ == \"__main__\":\n",
    "    # 创建查找器实例，设置最大范围为1000\n",
    "    finder = PerfectNumberFinder(1000)\n",
    "    # 查找所有完全数\n",
    "    finder.find_all()\n",
    "    # 显示结果\n",
    "    finder.display_results()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2e30dd1-028b-4574-b350-b3118d500f10",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cdeb053-cb43-43bb-a8c5-6da56120b87d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
